<?php

/**
 * ProductionDemand form.
 *
 * @package    ProdAssistant
 * @subpackage form
 * @author     Paweł Świtalla
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ProductionDemandForm extends BaseProductionDemandForm
{
  private $usedFields;
  private $categoryId;
  
  public function __construct($object = null, $fields = array(), $options = array()) {
    $this->usedFields = $fields;
    $this->categoryId = isset($options['categoryId']) ? $options['categoryId'] : false;
    $this->delivererId = isset($options['delivererId']) ? $options['delivererId'] : false;
    parent::__construct($object);
  }
  
  public function configure()
  {
    $this->validatorSchema->setOption('allow_extra_fields', true);
    //$this->validatorSchema->setOption('filter_extra_fields', false);
    
    if(!$this->categoryId && !$this->delivererId)
    {
      $query = Doctrine_Query::create()
              ->select('i.*')
              ->from('StoreItem i')
              ->orderBy('i.name ASC');
    }
    elseif($this->categoryId)
    {
      $query = Doctrine_Query::create()
              ->select('i.*')
              ->from('StoreItem i')
              ->where('i.category_id = ?', $this->categoryId)
              ->orderBy('i.name ASC');
      $this->setWidget('category_id', new sfWidgetFormInputHidden(array(), array('value' => $this->categoryId)));
    }
    elseif($this->delivererId)
    {
      $query = Doctrine_Query::create()
              ->select('i.*')
              ->from('StoreItem i')
              ->leftJoin('i.Category c')
              ->innerJoin('c.DelivererCategories dc')
              ->where('dc.deliverer_id = ?', $this->delivererId)
              ->orderBy('i.name ASC');
      $this->setWidget('deliverer_id', new sfWidgetFormInputHidden(array(), array('value' => $this->delivererId)));
    }
    $item = $query->fetchOne();
    
    if(!($item instanceof StoreItem)){
      //throw new Exception("Brak itemów w bazie");
      $this->setWidget('no_items', new sfWidgetFormInputHidden());
      $firstId = 0;
    }
    else{
      $firstId = $item->getId();
    }
    
    $this->getWidget('items_list')
            ->setOption('multiple', '')
            ->setOption('query', $query)
            ->setOption('method', 'getNameForDemand')
            ->setAttribute('name', 'production_demand[items_list][]')
            ->setAttribute('onchange', "var value = $(this).prop('value'); App.Form.updateAttribute('#items_list_values_1', 'name', 'production_demand[items_list_values]['+value+']')");
    
    $this->setWidget('items_list_values', new sfWidgetFormInput());
    
    $this->getWidget('items_list_values')
            ->setAttribute('id', 'items_list_values_1')
            ->setAttribute('name', 'production_demand[items_list_values]['.$firstId.']');
    
    if(count($this->usedFields) > 0)
    {
      $this->useFields($this->usedFields);
      if($this->getWidget('project_id') instanceof sfWidgetForm)
      {
        $core = PACore::getInstance();
        $this->getWidget('project_id')->setLabel('Projekt <br /><a onclick="App.loadWindow(\''.$core->generateAppUrl('Core', 'homeGetComponent', true, array('componentModule' => 'ProductionSettings', 'name' => 'manageProjects', 'handler' => 'refreshLastForm')).'\')" class="active"><img src="/images/icons/add.png" class="middle" height="18" width="18"></a>');
      }
    }
  }
}
